/**    
 * @{#} ZhengzhuangActivity.java Create on 2014-6-9 下午8:50:36    
 *          
 * @author <a href="mailto:evan0502@qq.com">Evan</a>   
 * @version 1.0    
 */
package com.smiier.skin;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import cn.o.app.json.JsonUtil;
import cn.skinapp.R;

import com.evan.common.handler.RequestHandler;
import com.evan.common.handler.callback.HandlerCallback;
import com.evan.common.task.RequestTask;
import com.evan.common.utils.CacheCommon;
import com.smiier.skin.constant.Constant;
import com.smiier.skin.net.SymptomPatientTask.SymptomItemInfo;
import com.smiier.skin.net.SymptomPatientTask.SymptomPatientItem;
import com.smiier.skin.net.SymptomPatientTask.SymptomPatientResponse;
import com.smiier.skin.ui.BasicActivity;
import com.smiier.skin.utils.CommonUtility;
import com.smiier.skin.vo.Zhengzhuang;
import com.umeng.analytics.MobclickAgent;

public class ZhengzhuangActivity extends BasicActivity implements
		OnItemClickListener {

	ListView list_zhengzhuang;
	private ArrayList<String> zhengzhuangMap = new ArrayList<String>();
	ArrayList<SymptomPatientItem> Symptoms = new ArrayList<SymptomPatientItem>();
	private ArrayList<String> list = new ArrayList<String>();

	@SuppressWarnings("unchecked")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_choose_zhengzhuang);

		init();
		setNavLeftBtn("取消");
		setNavLeftBtnNoDrawable();
		setNavRightBtn("确认");
		setNavTitle("选择症状");

		list_zhengzhuang = (ListView) findViewById(R.id.list_zhengzhuang);
	//	ArrayList<CacheCommon> caches = dbUtil.queryCache("Skilled");
//		if (caches.size() > 0) {

			String str = CommonUtility.getStringToBean(Symptoms,
					"Symptom_Patient.txt", getContext());
			try {
				Symptoms = JsonUtil.convert(str, SymptomPatientResponse.class);
				ArrayList<String> tmp = new ArrayList<String>();
				for (SymptomPatientItem item : Symptoms) {
					ArrayList<SymptomItemInfo> Symptoms = item.Symptoms;
					for (SymptomItemInfo info : Symptoms) {
						if(!tmp.contains(info.Doctor_Name)){
						tmp.add(info.Doctor_Name);
						}
					}
				}
				JSONArray array = JsonUtil.convertToArray(tmp);
				o(array);
			} catch (Exception e) {
				e.printStackTrace();
			}

	/*	} else {
			new RequestTask(activity, Constant.URL.REQUEST_SYMPTOM_JSON,
					new RequestHandler(activity, new HandlerCallback() {

						@Override
						public void callback(Object o) {
							if (!CommonUtility.isNull(o)) {
								try {
									JSONArray array = new JSONArray(
											o.toString());
									dbUtil.saveOrUpdate("Skilled", o);
									o(array);
								} catch (Exception e) {
									e.printStackTrace();
								}
							}
						}
					}), RequestTask.CHAR).execute();
		}*/
	}

	void o(JSONArray array) {
		try {
			for (int i = 0; i < array.length(); i++) {
				String str = array.getString(i);
				list.add(str);		
			}
			ZhengzhuangAdapter adapter = new ZhengzhuangAdapter(activity,
					list);
			list_zhengzhuang.setAdapter(adapter);
			list_zhengzhuang
					.setOnItemClickListener(ZhengzhuangActivity.this);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void onClick(View view) {
		super.onClick(view);
		int id = view.getId();
		if (id == R.id.text_right) {
			if (zhengzhuangMap.size() > 0) {
				Intent intent = new Intent();
				StringBuilder sb = new StringBuilder();
				for (String value : zhengzhuangMap) {
					sb.append(value).append(",");
				}
				if (sb.length() > 0 && sb.lastIndexOf(",") != -1) {
					intent.putExtra(Constant.IDENTITY_KEY,
							sb.substring(0, sb.lastIndexOf(",")));
				}
				setResult(Constant.ACTIVITY_CHOOSE_ZHENGZHUANG, intent);
				finish();
			} else {
				CommonUtility.tip(activity, "请选择症状");
			}
		}
	}

	CheckBox preCheckBox;

	@Override
	public void onItemClick(AdapterView<?> adapterView, View view, int arg2,
			long arg3) {
		CheckBox checkBox = (CheckBox) view.getTag();
		String zhengzhuang = (String) checkBox.getTag();
		if (!CommonUtility.isNull(checkBox)) {
			boolean isCheck = checkBox.isChecked();
			checkBox.setChecked(!isCheck);
			if (!isCheck) {
				zhengzhuangMap.add(zhengzhuang);
			} else {
				zhengzhuangMap.remove(zhengzhuang);
			}
		}
		if (!CommonUtility.isNull(preCheckBox) && preCheckBox != checkBox) {
			preCheckBox.setChecked(false);
			zhengzhuangMap.remove( preCheckBox.getTag());
		}
		preCheckBox = checkBox;
	}

	public static class ZhengzhuangAdapter extends BaseAdapter {

		private ArrayList<String> items;

		View[] views;
		LayoutInflater inflater;

		public ZhengzhuangAdapter(Context context, ArrayList<String> items) {
			this.items = items;
			views = new View[items.size()];
			inflater = LayoutInflater.from(context);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			convertView = views[position];
			if (!CommonUtility.isNull(convertView)) {
				return convertView;
			}
			String zhengzhuang = (String) getItem(position);
			convertView = inflater.inflate(R.layout.item_zhengzhuang, null);
			CheckBox checkBox = (CheckBox) convertView
					.findViewById(R.id.check_zhengzhuang);
			checkBox.setTag(zhengzhuang);
			convertView.setTag(checkBox);
			TextView name = (TextView) convertView.findViewById(R.id.text_name);
			TextView summary = (TextView) convertView
					.findViewById(R.id.text_summary);
			name.setText(zhengzhuang);
		//	summary.setText(zhengzhuang.summary);
			return convertView;
		}

		@Override
		public int getCount() {
			return items.size();
		}

		@Override
		public Object getItem(int position) {
			return items.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

	}

	/*
	 * @Override public void onResume() { super.onResume();
	 * MobclickAgent.onResume(getContext()); }
	 * 
	 * @Override public void onPause() { super.onPause();
	 * MobclickAgent.onPause(getContext()); }
	 */
}
